{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[22]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#########  百度语音识别库使用说明  #################\n",
    "# baidu_speech_2_word类提供：初始化方法和语音识别方法\n",
    "# init初始化方法：\n",
    "#        需要传入百度AI应用提供的APP_ID、API_KEY和SECRET_KEY\n",
    "# speech_2_word语音识别方法： \n",
    "#        识别的音频文件名，格式为.mp3\n",
    "#################################################\n",
    "\n",
    "#########  调用方式举例  #################\n",
    "# #导入标准库\n",
    "# import sys\n",
    "# from playsound import playsound\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../../baidu_api_lib')\n",
    "# from baidu_speech_recognition import baidu_speech_2_word\n",
    "\n",
    "# \"\"\" 我的 APPID AK SK \"\"\"\n",
    "# APP_ID = '20114704'\n",
    "# API_KEY = 'Lwd56jic7ZvTon8PsYyZ5DfG'\n",
    "# SECRET_KEY = 'EnNdR4Vr0HgATxz7LiHEDx3dKfLkWFGj'\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "#     try:\n",
    "#         #传入百度AI的参数\n",
    "#         sp_2_wd = baidu_speech_2_word(APP_ID, API_KEY, SECRET_KEY)\n",
    "        \n",
    "#         #对音频文件进行分析\n",
    "#         msg = sp_2_wd.speech_2_word('ppp1.mp3')\n",
    "        \n",
    "#         #输出分析结果\n",
    "#         print(msg)\n",
    "\n",
    "#     except KeyboardInterrupt:\n",
    "#         print(\"任务被终止了\")\n",
    "#################################################    \n",
    "\n",
    "from aip import AipSpeech\n",
    "\n",
    "class baidu_speech_2_word(object):\n",
    "    \n",
    "    #百度AI应用提供参数\n",
    "    APP_ID      = None\n",
    "    API_KEY     = None\n",
    "    SECRET_KEY  = None\n",
    "    client      = None\n",
    "    \n",
    "    def __init__(self, app_id, api_key, secret_key):\n",
    "        #获取提供的百度AI接口参数\n",
    "        self.APP_ID = app_id\n",
    "        self.API_KEY = api_key\n",
    "        self.SECRET_KEY = secret_key\n",
    "        \n",
    "        #获取授权\n",
    "        self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)\n",
    "        \n",
    "    def speech_2_word(self, sound_file, rate = 16000, dev_pid = 1537):\n",
    "        with open(sound_file, 'rb') as fp:\n",
    "            # 识别本地文件\n",
    "            return self.client.asr(fp.read(), 'pcm', rate, {'dev_pid': dev_pid,})\n",
    "        \n",
    "        return None\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
